package com.zmn.mcc.business.impl.dpermit.area;

import com.zmn.base.common.data.common.dro.area.AreaDRO;
import com.zmn.base.common.data.dubbo.interfaces.area.AreaListRemoteService;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.mcc.business.interfaces.dpermit.area.DpermitAreaBService;
import com.zmn.mcc.common.constant.DpermitConsts;
import com.zmn.mcc.manager.utils.DpermitUtil;
import com.zmn.mcc.model.dto.dpermit.DpermitSimpleDO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author heguanghua
 * @since 2020/05/18 15:58
 */
@Service
public class DpermitAreaBServiceImpl implements DpermitAreaBService {
    protected Logger logger = LoggerFactory.getLogger(DpermitAreaBServiceImpl.class);

    @DubboReference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION, check = false)
    private AreaListRemoteService areaListRemoteService;


    // region 取得全部省、市、区县列表，省份和城市并列，区县为城市的子集

    @Override
    @ReadOnlyConnection
    public List<DpermitSimpleDO> listAreaAndDpermitChecked(Integer status, List<Integer> dpermitIds) {

        List<DpermitSimpleDO> items = new ArrayList<>();

        // 区域ID和简单区域对象的映射关系
        Map<Integer, DpermitSimpleDO> map = new HashMap<>(16);

        // 省、市、区县列表
        ResponseDTO<List<AreaDRO>> areasDROList = areaListRemoteService.listAboveByLevelAndStatus(DpermitConsts.AREA_LEVEL_COUNTY, status);
        List<AreaDRO> areas = new ArrayList<>();
        if (areasDROList.isSuccess()) {
            areas.addAll(areasDROList.getData());
        }
        for (AreaDRO area : areas) {

            // 防止数据错误抛出异常
            if (area.getLevel() == null) {
                continue;
            }

            // 简单区域对象
            DpermitSimpleDO simple = this.parseToDpermitSimpleDO(area);

            // 加入映射关系map
            if (area.getLevel() == DpermitConsts.AREA_LEVEL_CITY) {
                map.put(simple.getDataId(), simple);
            }

            // 选中已分配
            if (CollectionUtil.isNotNullOrEmpty(dpermitIds) && dpermitIds.contains(simple.getDataId())) {
                simple.setChecked(true);
            }

            // 省、市加入主列表
            if (area.getLevel() <= DpermitConsts.AREA_LEVEL_CITY) {
                items.add(simple);
            }
            // 区县加入市级区域的子集
            else if (area.getLevel() == DpermitConsts.AREA_LEVEL_COUNTY) {
                DpermitSimpleDO parent = map.get(simple.getParentId());
                if (parent != null) {
                    parent.getSubors().add(simple);
                }
            }
        }

        return items;
    }
    // endregion

    // region 取得全部省、市、区县的层级列表

    @Override
    public List<DpermitSimpleDO> listAreaTreeAndDpermitChecked(Integer status, List<Integer> dpermitIds) {

        List<DpermitSimpleDO> items = new ArrayList<>();

        // 区域ID和简单区域对象的映射关系
        Map<Integer, DpermitSimpleDO> map = new HashMap<>(16);

        // 省、市、区县列表
        ResponseDTO<List<AreaDRO>> areasDROList = areaListRemoteService.listAboveByLevelAndStatus(DpermitConsts.AREA_LEVEL_COUNTY, status);
        List<AreaDRO> areas = new ArrayList<>();
        if (areasDROList.isSuccess()) {
            areas.addAll(areasDROList.getData());
        }
        for (AreaDRO area : areas) {

            // 防止数据错误抛出异常
            if (area.getLevel() == null) {
                continue;
            }

            // 简单区域对象
            DpermitSimpleDO simple = this.parseToDpermitSimpleDO(area);

            // 省、市加入映射关系map
            if (area.getLevel() <= DpermitConsts.AREA_LEVEL_CITY) {
                map.put(simple.getDataId(), simple);
            }

            // 选中已分配
            if (CollectionUtil.isNotNullOrEmpty(dpermitIds) && dpermitIds.contains(simple.getDataId())) {
                simple.setChecked(true);
            }

            // 省份加入主列表
            if (area.getLevel() <= DpermitConsts.AREA_LEVEL_PROVINCE) {
                items.add(simple);
            }
            // 加入上级区域的子集
            else if (area.getLevel() >= DpermitConsts.AREA_LEVEL_CITY) {
                DpermitSimpleDO parent = map.get(simple.getParentId());
                if (parent != null) {
                    parent.getSubors().add(simple);
                }
            }
        }

        /**
         * 检查并处理简单数据对象选中状态
         * 下级部分选中则上级选中，下级全部选中则对上级标识
         */
        DpermitUtil.checkSelected(items, false);
        return items;
    }
    // endregion


    /**
     * 转换为简单数据权限对象
     *
     * @param area
     * @return
     */
    private DpermitSimpleDO parseToDpermitSimpleDO(AreaDRO area) {
        if (area == null) {
            return null;
        }
        DpermitSimpleDO simple = new DpermitSimpleDO();
        simple.setDataId(area.getAreaId());
        simple.setDataName(area.getName());
        simple.setParentId(area.getParentId());
        simple.setStatus(area.getStatus());
        simple.setLevel(area.getLevel());
        return simple;
    }
}
